﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;

public partial class home_User : System.Web.UI.Page
{
    int page_current = 1;
    protected void Page_Load(object sender, EventArgs e)
    {
        // only manager is enable to access this page
        if (Session["id_user"] == null || Session["permission"] == null || ((int)Session["permission"]) <= 1 
            || ((int)Session["permission"])>=4)
            Response.Redirect("../login.aspx");

        #region get current page
        if (Request.QueryString["page"] != null)
        {
            try
            {
                page_current = Int32.Parse(Request.QueryString["page"]);
            }
            catch
            {
                page_current = 1;
            }
        }
        #endregion

        if (!IsPostBack)
        {
            #region bind data
            string strOrderby = orderby.SelectedValue;
            if (Session["orderby_user"] != null)
            {
                strOrderby = Session["orderby_user"].ToString();
                orderby.Items.FindByValue(orderby.SelectedValue).Selected = false;
                orderby.Items.FindByValue(strOrderby).Selected = true;
            }
            string strOrder = order.SelectedValue;
            if (Session["order_user"] != null)
            {
                strOrder = Session["order_user"].ToString();
                order.Items.FindByValue(order.SelectedValue).Selected = false;
                order.Items.FindByValue(strOrder).Selected = true;
            }
            BindData(strOrderby, strOrder);
            #endregion
        }
    }

    private void BindData(string orderby, string order)
    {
        int page_number = 0;
        // load list of project
        if (Session["id_user"] != null && Session["permission"] != null)
        {
            SqlParameter[] listParams = new SqlParameter[6];
            //-- param 1: @id_user
            int id = Int32.Parse(Session["id_user"].ToString());
            SqlParameter param = new SqlParameter("@userId", id);
            param.SqlDbType = SqlDbType.Int;
            param.Direction = ParameterDirection.Input;
            listParams[0] = param;

            //-- param 2: @page_size
            param = new SqlParameter("@page_size", 15);
            param.SqlDbType = SqlDbType.Int;
            param.Direction = ParameterDirection.Input;
            listParams[1] = param;

            //-- param 3: @page_current
            param = new SqlParameter("@page_current", page_current);
            param.SqlDbType = SqlDbType.Int;
            param.Direction = ParameterDirection.Input;
            listParams[2] = param;

            // -- param 4: @orderby
            param = new SqlParameter("@orderby", orderby);
            param.SqlDbType = SqlDbType.VarChar;
            param.Size = 50;
            param.Direction = ParameterDirection.Input;
            listParams[3] = param;

            // -- param 5: @typeorder
            param = new SqlParameter("@typeorder", order);
            param.SqlDbType = SqlDbType.VarChar;
            param.Size = 50;
            param.Direction = ParameterDirection.Input;
            listParams[4] = param;

            //-- param 6: @page_number
            param = new SqlParameter();
            param.ParameterName = "@page_number";
            param.SqlDbType = SqlDbType.Int;
            param.Direction = ParameterDirection.Output;
            listParams[5] = param;

            DataTable dtTable = dal.SelectTableSP("proAllUser_SEL", listParams);
            page_number = Int32.Parse(listParams[5].Value.ToString());

            // display full info of catalogs in gridview

            users.DataSource = dtTable;
            users.DataBind();
        }

        // display pager
        if (page_number == 0)
        {
            display.Text = "Chưa có người dùng nào !";
            pager.Visible = false;
            this.order.Visible = false;
            this.orderby.Visible = false;
            this.label1.Visible = false;
            this.label2.Visible = false;

        }
        else if (page_number == 1)
        {
            pager.Visible = false;
            display.Text = "";
        }
        else if (page_number > 1)
        {
            display.Text = "";
            // load pager
            pager.link = "~/home/user.aspx";
            pager.numberpage = page_number;
            pager.currentpage = page_current;
            pager.paramPageName = "page";
            pager.Visible = true;
            pager.BindData();
        }
    }
    protected void orderby_SelectedIndexChanged(object sender, EventArgs e)
    {
        string strOrderby = ((DropDownList)sender).SelectedValue;
        Session.Add("orderby_user", strOrderby);
        Session.Timeout = 60;
        string strOrder = order.SelectedValue;
        BindData(strOrderby, strOrder);
    }
    protected void order_SelectedIndexChanged(object sender, EventArgs e)
    {
        string strOrder = ((DropDownList)sender).SelectedValue;
        Session.Add("order_user", strOrder);
        Session.Timeout = 60;
        string strOrderby = orderby.SelectedValue;
        BindData(strOrderby, strOrder);
    }
    protected void btOk_Click(object sender, EventArgs e)
    {
        string strOrderby = orderby.SelectedValue;
        if (Session["orderby_user"] != null)
        {
            strOrderby = Session["orderby_user"].ToString();
            orderby.Items.FindByValue(orderby.SelectedValue).Selected = false;
            orderby.Items.FindByValue(strOrderby).Selected = true;
        }
        string strOrder = order.SelectedValue;
        if (Session["order_user"] != null)
        {
            strOrder = Session["order_user"].ToString();
            order.Items.FindByValue(order.SelectedValue).Selected = false;
            order.Items.FindByValue(strOrder).Selected = true;
        }
        BindData(strOrderby, strOrder);
    }
    protected void users_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        //display.Text = e.CommandArgument.ToString();
        if (e.CommandName == "removeuser")
        {
            // get permission of userId
            // if it is register => can't delete the user
            int userId = Int32.Parse(e.CommandArgument.ToString());
            string sql = "SELECT parentId FROM [user] WHERE id=" + comm.to_sql(userId.ToString(), "number");
            DataTable dtTable = dal.SelectTable(sql);
            if (dtTable.Rows.Count != 1)
                return;
            if (Int32.Parse(dtTable.Rows[0]["parentId"].ToString()) != 0)
            {
                sql = "DELETE FROM [user] WHERE id=" + comm.to_sql(e.CommandArgument.ToString(), "number");
                dal.Execute(sql);
                string strOrderby = (Session["orderby_user"] != null ? Session["orderby_user"].ToString() : orderby.SelectedValue);
                string strOrder = (Session["order_user"] != null ? Session["order_user"].ToString() : order.SelectedValue);
                BindData(strOrderby, strOrder);

            }
        }
    }
}
